package cn.jantd.modules.task.controller;

import cn.jantd.core.annotation.AutoLog;
import cn.jantd.core.api.vo.Result;
import cn.jantd.core.system.query.QueryGenerator;
import cn.jantd.modules.activiti.entity.ActModel;
import cn.jantd.modules.system.entity.SysRole;
import cn.jantd.modules.task.entity.TaskInfo;
import cn.jantd.modules.task.service.ITaskInfoService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.activiti.engine.repository.Model;
import org.activiti.engine.repository.ModelQuery;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.ReportAsSingleViolation;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @ProjectName: trial-training-flow-boot-parent
 * @Package: cn.jantd.modules.task.controller
 * @ClassName: TaskInfo
 * @Author: admin
 * @Description: 任务管理-任务信息
 * @Date: 2020/7/9 17:17
 * @Version: 1.0
 */

@RestController
@RequestMapping("/sys/taskInfo")
@Slf4j
public class TaskInfoController {
    @Autowired
    ITaskInfoService taskInfoService;
    /**
     * @Author:     风中的那朵云
     * @Description:  任务信息分页查询
     * @Date:    2020/5/6
     * @Version:    1.0
     */
    @GetMapping(value = "/list")
    public Result<IPage<TaskInfo>> queryPageList(TaskInfo taskInfo,
                                                @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                                HttpServletRequest req) {
        Result<IPage<TaskInfo>> result = new Result<IPage<TaskInfo>>();
        QueryWrapper<TaskInfo> queryWrapper = QueryGenerator.initQueryWrapper(taskInfo, req.getParameterMap());
        Page<TaskInfo> page = new Page<TaskInfo>(pageNo, pageSize);
        IPage<TaskInfo> pageList = taskInfoService.page(page, queryWrapper);
        result.setSuccess(true);
        result.setResult(pageList);
        return result;
    }

    /**
     * 添加
     *
     * @param taskInfo
     * @return
     */
    @ResponseBody
    @PostMapping(value = "/addTask")
    public Result<TaskInfo> add(@RequestBody TaskInfo taskInfo) {
        Result<TaskInfo> result = new Result<TaskInfo>();
        try {
            taskInfo.setCreateTime(new Date());
            taskInfoService.save(taskInfo);
            result.success("添加成功！");
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            result.error500("操作失败");
        }
        return result;
    }


    /**
     * 编辑
     *
     * @param taskInfo
     * @return
     */
    @ResponseBody
    @PutMapping(value = "/editTask")
    public Result<TaskInfo> edit(@RequestBody TaskInfo taskInfo) {
        Result<TaskInfo> result = new Result<TaskInfo>();
        TaskInfo taskInfo1 = taskInfoService.getById(taskInfo.getId());
        if (taskInfo1 == null) {
            result.error500("未找到对应实体");
        } else {
            taskInfo.setUpdateTime(new Date());
            boolean ok = taskInfoService.updateById(taskInfo);
            if (ok) {
                result.success("修改成功!");
            }
        }

        return result;
    }

    /**
     * 删除
     *
     * @param id
     * @return
     */
    @ResponseBody
    @DeleteMapping(value = "/delete")
    public Object delete(String id) {
        Result result = new Result();
        Boolean b = taskInfoService.removeById(id);
        if (b == true) {
            result.success("删除成功!");
        } else {
            result.error500("删除失败");
        }
        return result;
    }
}
